package net.hn.hnms.biz.emgcy.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.emgcy.domain.ExpertPo;
import net.hn.hnms.biz.emgcy.domain.dto.ExpertDto;
import net.hn.hnms.biz.emgcy.domain.vo.ExpertVo;
import net.hn.hnms.biz.emgcy.listener.ExpertImportListener;
import net.hn.hnms.biz.emgcy.service.ExpertService;
import net.hn.hnms.biz.emgcy.template.ExpertTemplate;
import net.hn.hnms.biz.emgcy.util.ExcelUtils;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import net.hn.hnms.sys.system.api.RemoteDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: baijiajun
 * @Date: 2022/9/13
 */
@Api(value = "应急专家模块", tags = {"应急专家"})
@RestController
@RequestMapping("expert")
public class ExpertController {
    @Autowired
    private ExpertService expertService;
    @Autowired
    private RemoteDictService remoteDictService;
    @ApiOperation("应急专家列表")
    @GetMapping("pageList")
    @AutoSetMineCode
    public R<ResponsePage<ExpertVo>> pageList(PageQuery pageQuery, ExpertDto expertDto) {
        ResponsePage<ExpertVo> list=expertService.pageList(pageQuery,expertDto);
        return R.ok(list);
    }

    @ApiOperation("应急专家添加")
    @PostMapping("/add")
    public R add(@RequestBody ExpertPo expertPo){
        Boolean flag = expertService.add(expertPo);
        if(flag){
            return R.ok("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation("应急专家修改")
    @PutMapping("/update")
    public R update(@RequestBody ExpertPo expertPo){
        Boolean flag = expertService.update(expertPo);
        if(flag){
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }
    @ApiOperation("应急专家删除")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Long id){
        Boolean flag = expertService.delete(id);
        if(flag){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

    @ApiOperation(value = "获取救援专家列表列表导出", position = 1)
    @GetMapping("export")
    public void export(HttpServletResponse httpServletResponse) throws IOException {
        expertService.export(httpServletResponse);
    }

    /**
     * 导出模板
     */
    @GetMapping(value = "exportTemplate")
    public void exportTemplate(HttpServletResponse response) throws IOException {
        List<ExpertTemplate> list = new ArrayList<>();
        ExcelUtils.exportExcel(response, "救援专家导出模板", ExpertTemplate.class, list);
    }

    /**
     * 导入
     */
    @PostMapping(value = "import")
    public void testImport(MultipartFile file) {
        ExpertImportListener expertImportListener = new ExpertImportListener(remoteDictService,expertService);
        ExcelUtils.importExcel(file, ExpertTemplate.class, expertImportListener);
    }
}
